package org.albino.evinreisi.facebook;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

public class Util {
	public static String readUrlIntoString(String urlString) throws IOException {
		URL url = new URL(urlString);
		BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

		String response = "";
		String inputLine;

		while ((inputLine = in.readLine()) != null)
			response += inputLine;

		in.close();
		return response;
	}

	public static String friendsToCSV(List<FacebookUser> friends){
		String csv = "";
		
		for(int i = 0; i<friends.size(); i++){
			csv += friends.get(i).getId();
			
			if(i<friends.size()-1)
				csv += ",";
		}
		
		return csv;
	}
	
	public static String friendIdsToCSV(List<String> friendIds){
		String csv = "";
		
		for(int i = 0; i<friendIds.size(); i++){
			csv += friendIds.get(i);
			
			if(i<friendIds.size()-1)
				csv += ",";
		}
		
		return csv;
	}
	
	public static List<String> friendIdsFromCSV(String csv){
		List<String> friendIds = new ArrayList<String>();
		for(String id : StringUtils.split(csv, ","))
			friendIds.add(id);
		
		return friendIds;
	}
	
	public static String formatDate(Date date){
		if(date == null)
			return null;
		return new SimpleDateFormat("E, dd MMM yyyy HH:mm").format(date); 		
	}
	
	public static String getChannelKeyFromUserId(String userId){
		return userId;
	}
}
